package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
import ModelLayer.*;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 

    public DvdUI()
    {
        dvdCtr = new DVDCtr();
    }

    public void start(DVDCtr ctr)
    {
        dvdCtr= ctr;
        dvdMenu();
    }

    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeDVDMenu();
            if (choice == 1)
            {
                printDVD();
            }
            else{if(choice == 2){
                    createDVD();
                }
                else{
                    if(choice == 3){
                        createCopy();
                    }
                    else{
                        if(choice == 4){
                            deleteDVD();
                        }
                        else{
                            if(choice == 5){
                                listAllDvds();
                            }
                            else
                            {
                                if(choice == 7)
                                {
                                    listAllCopies();
                                }
                                else
                                {          
                                    if(choice == 8)
                                    {
                                        deleteCopy();
                                    }
                                    else{
                                        exit = true;
                                    }//end else
                                }
                            }
                        }//end else
                    }//end else
                }//end else
            }//end else

        }//end while
    }

    private int writeDVDMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** DVD Menu ***");
        System.out.println(" (1) Show DVD information");
        System.out.println(" (2) Create DVD description");
        System.out.println(" (3) Create DVD specimen");
        System.out.println(" (4) Delete DVD");
        System.out.println(" (5) Show current DVD list");
        System.out.println(" (6) Return");
        System.out.println(" (7) Show current Copies");
        System.out.println(" (8) Delete copy");
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    private int inputDVDNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type DVD number: ");
        int no = keyboard.nextInt();
        return no;
    }

    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }

    private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Type the copy number: ");
        int no = keyboard.nextInt();
        return no;
    }

    //there is need for more methods to read information about DVDs
    //and methods to display the information

    private DVD findDVD()
    {     
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write id: ");
        long id = keyboard.nextInt();

        DVD xDvd = dvdCtr.findDVD(id);
        if(xDvd != null)
        {
            return xDvd;
        }

        else
        {
            return null;
        }

        //int no = inputDVDNo();
        //is to be changed when the DVD controller is completed
        //String dvd = dvdCtr.findDVD(no);
    }

    public void printDVD()
    {
        DVD xDvd = findDVD();

        System.out.println("Id: " + xDvd.getId());
        System.out.println("Title: " + xDvd.getTitle());
        System.out.println("Artist: " + xDvd.getArtist());
        System.out.println("Publication Date: " + xDvd.getPublicationDate());
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            if(choice == 6)
            {

                exit = true;

                if(choice == 6)
                {
                    exit = true;
                }

            }
        }

    }

    private void createDVD()
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter id: ");
        long id = input.nextLong();
        System.out.println("Enter title: ");
        String title = input.next();
        System.out.println("Enter artist: ");
        String artist = input.next();
        System.out.println("Enter Publication Date: ");
        String publicationDate = input.next();

        dvdCtr.createDVD(id, title, artist, publicationDate);

        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            if(choice == 6)
            {
                exit = true;
            }
        }

    }

    private void createCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write id: ");
        long id = keyboard.nextInt();

        DVD xDvd = dvdCtr.findDVD(id);

        if(xDvd != null)
        {
            System.out.println(" Enter date: ");
            String date = keyboard.next();
            System.out.println(" Enter price: ");
            String price = keyboard.next();

            dvdCtr.createCopy(xDvd.getId(), date, price);
        }
        else
        {
            System.out.println(" No DVD's has been found!");

            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            int choice = keyboard.nextInt();

            boolean exit = false;
            while(!exit)//that is: while exit is false
            {
                if(choice == 6)
                {
                    exit = true;
                }
            }
        }
    }

    private void deleteDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write id: ");
        long id = keyboard.nextInt();

        dvdCtr.deleteDVD(id);  

        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();

        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            if(choice == 6)
            {
                exit = true;
            }
        }
    }

    private void deleteCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Serial nr.: ");
        long sn = keyboard.nextInt();

        dvdCtr.deleteCopy(sn);  

        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();

        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            if(choice == 6)
            {
                exit = true;
            }
        }
    }

    private void listAllDvds()
    {
        dvdCtr.listAllDvds();

        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            if(choice == 6)
            {
                exit = true;
            }
        }
    }

    private void listAllCopies()
    {
        dvdCtr.listAllCopies();

        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            if(choice == 6)
            {
                exit = true;
            }
        }
    }
}